package com.paradogs.framework.server;

import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
import com.paradogs.common.utils.ProtoUtils;
import io.netty.buffer.ByteBuf;
import lombok.Data;

/**
 * @author: yumi
 * @date: 2023/12/26  15:42
 * @Description: TODO
 */
@Data
@ProtobufClass
public class MessageHeader {
    private Long id; // 回调时作为回复的 ID 使用
    private String server; // 目标服务器标志
    private String route; // 目标路由
    private Long replyId; // 回复的消息 ID
    // 待优化：下面 2 个参数 gc 给客户端的时候可以不传
    private String channelId; // 玩家连接标识
    private Long uid; // 玩家 ID，登录的才有

    public MessageHeader() {
//        this.id = new Random().nextLong();
    }

    public void serialization(ByteBuf out) {
        byte[] bytes = ProtoUtils.encode(this);
        out.writeShort(bytes.length);
        out.writeBytes(bytes);
    }

    public void deserialization(ByteBuf in) {
        byte[] bytes = new byte[in.readShort()];
        in.readBytes(bytes);
        this.deserialization(bytes);
    }

    public void deserialization(byte[] bytes) {
        MessageHeader header = ProtoUtils.decode(bytes, this.getClass());
        this.id = header.id;
        this.server = header.server;
        this.route = header.route;
        this.replyId = header.replyId;
        this.channelId = header.channelId;
        this.uid = header.uid;
    }
}
